<template>
  <AMISRenderer :schema="schema" />
</template>

<script setup>
import AMISRenderer from '@/components/AMISRenderer.vue'
import resetPasswordPage from './resetPassword'
import editPage from './edit'
import addPage from './add'
import detailPage from './detail'

const schema = {
  type: 'page',
  body: {
    type: 'crud',
    api: '/upms/admin/user/list',
    syncLocation: false,
    autoFillHeight: true,
    footerToolbar: [
      'statistics',
      'switch-per-page',
      'pagination'
    ],
    autoGenerateFilter: {
      showBtnToolbar: false,
      columnsNum: 4
    },
    headerToolbar:[
      {
        type: 'button',
        actionType: 'dialog',
        align: 'right',
        label: '新增',
        icon: 'fa fa-plus pull-left',
        primary: true,
        dialog: {
          title: '新增用户',
          closeOnEsc: true,
          showErrorMsg: false,
          size: 'md',
          body: addPage
        }
      }
    ],
    columns: [
      {
        name: 'username',
        label: '用户名',
        searchable: {
          type: 'input-text',
          name: 'username',
          label: '用户名',
          clearable: true
        }
      },
      {
        name: 'typeStr',
        label: '用户类型',
        searchable: {
          type: 'select',
          name: 'type',
          label: '用户类型',
          source: '/base/dict/options/user-type',
          clearable: true
        }
      },
      {
        name: 'mainFlag',
        label: '主子账号',
        type: 'mapping',
        map: {
          "1": "<span class='label label-success'>主账号</span>",
        }
      },
      {
        name: 'orgName',
        label: '所属机构',
        searchable: {
          type: 'input-text',
          name: 'orgName',
          label: '所属机构',
          clearable: true
        }
      },
      {
        name: 'phone',
        label: '绑定手机号',
        searchable: {
          type: 'input-text',
          name: 'phone',
          label: '绑定手机号',
          clearable: true
        }
      },
      {
        name: 'lastLoginDate',
        label: '最后登录时间',
      },
      {
        type: 'switch',
        name: 'status',
        label: '状态',
        onText: '启用',
        offText: '禁用',
        trueValue: 1,
        falseValue: 0,
        disabledOn: '${id == "1"}',
        onEvent:{
          change:{
            actions:[
              {
                actionType: 'ajax',
                api:{
                  url:'/upms/admin/user/update/status',
                  method: 'post',
                  requestAdaptor: function (api, context){
                    return {
                      ...api,
                      data: {
                        id: context.id,
                        status: context.status
                      }
                    };
                  }
                }
              }
            ]
          }
        }
      },
      {
        type: 'operation',
        label: '操作',
        fixed: 'right',
        buttons: [
          {
            label: '修改',
            type: 'button',
            actionType: 'dialog',
            visibleOn: '${id != "1"}',
            dialog:{
              title: '修改用户',
              closeOnEsc: true,
              showErrorMsg: false,
              body: editPage,
              size: 'md'
            }
          },
          {
            label: '重置密码',
            type: 'button',
            actionType: 'dialog',
            visibleOn: '${id != "1"}',
            dialog:{
              title: '重置密码',
              closeOnEsc: true,
              showErrorMsg: false,
              body: resetPasswordPage
            }
          },
          {
            label: '删除',
            type: 'button',
            level: 'danger',
            actionType:'ajax',
            confirmText: '确认要删除该记录？',
            visibleOn: '${id != "1"}',
            api: {
              url: '/upms/admin/user/delete?id=${id}',
              method: 'post',
              data:{}
            },
          }
        ]
      }
    ]
  }
}

</script>
